/********************************Copyright (c)**********************************\
**
**                   (c) Copyright 2023, Main, China, QD.
**                           All Rights Reserved
**
**                                 By(wo4fisher)
**                           http://www.wo4fisher.com
**
**----------------------------------文件信息------------------------------------
** 文件名称: eeprom.c
** 创建人员: wht
** 创建日期: 2023-10-20
** 文档描述:
**
**----------------------------------版本信息------------------------------------
** 版本代号: V0.1
** 版本说明: 初始版本
**
**------------------------------------------------------------------------------
\********************************End of Head************************************/
#include "eeprom.h"
#include "i2c.h"
#include "stdio.h"

/*******************************************************************************
** 函数名称: Bsp_I2C_WriteFloat
** 功能描述:
** 参数说明: f: [输入/出]
** 返回说明: None
** 创建人员: wht
** 创建日期: 2023-10-22
**------------------------------------------------------------------------------
** 修改人员:wht
** 修改日期:
** 修改描述:
**------------------------------------------------------------------------------
********************************************************************************/
void Bsp_I2C_WriteFloat ( uint16_t addr, float f )
{
    uint8_t t;
    HAL_GPIO_WritePin ( I2C1_WP_EE_GPIO_Port,I2C1_WP_EE_Pin,GPIO_PIN_RESET );
		
    t = ( uint8_t ) ( f*10 ); //放大10倍，取整；取整后不大于255

		if ( HAL_I2C_Mem_Write ( &hi2c1, ( uint16_t ) 0xA0,( uint16_t ) addr,I2C_MEMADD_SIZE_8BIT, ( uint8_t * ) &t,1,200 ) != HAL_OK )
        printf ( "write to 24c02 failed." );
		
    HAL_GPIO_WritePin ( I2C1_WP_EE_GPIO_Port,I2C1_WP_EE_Pin,GPIO_PIN_SET );
}

/*******************************************************************************
** 函数名称: Bsp_I2C_ReadFloat
** 功能描述:
** 参数说明: None
** 返回说明: None
** 创建人员: wht
** 创建日期: 2023-10-22
**------------------------------------------------------------------------------
** 修改人员:wht
** 修改日期:
** 修改描述:
**------------------------------------------------------------------------------
********************************************************************************/
float Bsp_I2C_ReadFloat ( uint16_t addr )
{
    uint8_t q=0;
    float f;
    if ( HAL_I2C_Mem_Read ( &hi2c1, ( uint16_t ) 0xA0,( uint16_t ) addr,I2C_MEMADD_SIZE_8BIT, ( uint8_t * ) &q,1,200 ) != HAL_OK )
        printf ( "read from 24c02 failed." );

    f= ( ( float ) q ) /10.0;
    return f;
}


/*******************************************************************************
** 函数名称: Bsp_I2C_WriteInt
** 功能描述: 
** 参数说明: addr: [输入/出] 
**			 p: [输入/出] 
** 返回说明: None
** 创建人员: wht
** 创建日期: 2023-10-24
**------------------------------------------------------------------------------
** 修改人员:wht
** 修改日期:
** 修改描述:
**------------------------------------------------------------------------------
********************************************************************************/
void Bsp_I2C_WriteInt ( uint16_t addr,uint16_t p )
{
    uint8_t t;
    HAL_GPIO_WritePin ( I2C1_WP_EE_GPIO_Port,I2C1_WP_EE_Pin,GPIO_PIN_RESET );

    t = ( uint8_t ) ( p&0x00ff );//低8位
		printf("write L8 = %d\n",t);
    if ( HAL_I2C_Mem_Write ( &hi2c1, ( uint16_t ) 0xA0, ( uint16_t ) addr,I2C_MEMADD_SIZE_8BIT, ( uint8_t * ) &t,1,200 ) != HAL_OK )
        printf ( "write to 24c02 failed." );
		
		HAL_Delay(5);
    
		t = ( uint8_t ) (( p>>8)&0x00ff);//高8位
printf("write H8 = %d\n",t);
    if ( HAL_I2C_Mem_Write ( &hi2c1, ( uint16_t ) 0xA0, ( uint16_t ) ( addr+1 ),I2C_MEMADD_SIZE_8BIT, ( uint8_t * ) &t,1,200 ) != HAL_OK )
        printf ( "write to 24c02 failed." );

    HAL_GPIO_WritePin ( I2C1_WP_EE_GPIO_Port,I2C1_WP_EE_Pin,GPIO_PIN_SET );
}

/*******************************************************************************
** 函数名称: Bsp_I2C_ReadInt
** 功能描述: 
** 参数说明: addr: [输入/出] 
** 返回说明: None
** 创建人员: wht
** 创建日期: 2023-10-24
**------------------------------------------------------------------------------
** 修改人员:wht
** 修改日期:
** 修改描述:
**------------------------------------------------------------------------------
********************************************************************************/
uint16_t Bsp_I2C_ReadInt ( uint16_t addr )
{
    uint8_t t=0;
    uint16_t p;
    if ( HAL_I2C_Mem_Read ( &hi2c1, ( uint16_t ) 0xA0, ( uint16_t ) ( addr-1 ),I2C_MEMADD_SIZE_8BIT, ( uint8_t * ) &t,1,200 ) != HAL_OK )
        printf ( "read from 24c02 failed." );
		printf("read H8 = %d\n",t);
		HAL_Delay(5);
    p = t;
    if ( HAL_I2C_Mem_Read ( &hi2c1, ( uint16_t ) 0xA0, ( uint16_t ) addr,I2C_MEMADD_SIZE_8BIT, ( uint8_t * ) &t,1,200 ) != HAL_OK )
        printf ( "read from 24c02 failed." );
		printf("read L8 = %d\n",t);
    p= ( p<<8 ) |t;
		    if ( HAL_I2C_Mem_Read ( &hi2c1, ( uint16_t ) 0xA0, ( uint16_t ) (addr+1),I2C_MEMADD_SIZE_8BIT, ( uint8_t * ) &t,1,200 ) != HAL_OK )
        printf ( "read from 24c02 failed." );
		printf("read L8 = %d\n",t);
				    if ( HAL_I2C_Mem_Read ( &hi2c1, ( uint16_t ) 0xA0, ( uint16_t ) (addr+2),I2C_MEMADD_SIZE_8BIT, ( uint8_t * ) &t,1,200 ) != HAL_OK )
        printf ( "read from 24c02 failed." );
		printf("read L8 = %d\n",t);
    return p;
}
/********************************End of File************************************/
